package com.cn.zengzeng.spbzengzeng.test;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @Description:
 * @Author: zhangzengzeng
 * @Date: 2021/11/5 16:57
 */
@Component
@Slf4j
public class CallbackSender implements RabbitTemplate.ConfirmCallback {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
    * @Auther zhangzengzeng
    * @Date 2021/11/5 17:30
    * @param exchange 交换机
    * @param topic 队列
    * @param obj 消息体
    */
    public void send(String exchange,String topic,Object obj){
        rabbitTemplate.setConfirmCallback(this);
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        log.info("callbacksender UUID:"+correlationData);
        this.rabbitTemplate.convertAndSend(exchange,topic,obj,correlationData);
    }

    /**
    * @Auther zhangzengzeng
    * @Date 2021/11/5 17:31
    * @param correlationData 消息唯一标识
    * @param ack  发送结果
    * @param cause 失败原因
    */
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        if(correlationData!=null){
            log.error("callback confirm===correlationData:{},ack:{},cause:{}"+correlationData.getId(),ack,cause);
        }
    }
}